
package com.fwzc.giuseppe.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;


public class FileHelper {
	
	
	private SharedPreferences sharePref;
	public FileHelper(Context context,String fileName){
		sharePref=context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
	}


	public String getShareProf(String key){
		return sharePref.getString(key, "");
	}
	public void putShareProf(String key,String value){
		sharePref.edit().putString(key, value).commit();
	}
	
	/**
	 * 将json数据以键值对形式存储下来
	 * @param context
	 * @param fileName
	 * @param json
	 */
	public static void savePrefByKeyValue(Context context ,String fileName,String json){
		if(json.length()==0)
			return ;
		SharedPreferences sp=context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		Editor edite=sp.edit();
		JSONArray array = new JSONArray();
		try{
			if(json.trim().startsWith("[")&&json.trim().endsWith("]")){
				array =new JSONArray(json);
			}else if(json.trim().startsWith("{")&&json.trim().endsWith("}")){
				array.put(new JSONObject(json));
			}
			for (int i = 0; i < array.length(); i++) {
				Iterator<?> it = array.getJSONObject(i).keys();
				while (it.hasNext()) {
					String key = (String) it.next();
					String value= array.getJSONObject(i).getString(key);
					edite.putString(key,value);
				}
			}
		}catch (Exception e) {
		}
		edite.commit();
	}
	public static  String getShareProf(Context context,String fileName,String key){
		SharedPreferences sp=context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getString(key, "");
	}
	public static void putShareProf(Context context,String fileName,String key,String value){
		SharedPreferences sp=context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		sp.edit().putString(key, value).commit();
	}
	public static void deletedSpFile(Context context,String spFileName){
		SharedPreferences sharePref=context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
		sharePref.edit().clear().commit();
	}
	
	public static String saveBitmap(Bitmap bitmap,String path) {
		String fileName="img_"+System.currentTimeMillis()+".jpg";
//		File file=new File(Config.DISK_CACHE_CAMERA);
		File file=new File(path);
		if(!file.exists()){
			file.mkdir();
		}
		File imgFile=new File(file,fileName);
		try{
			imgFile.createNewFile();
			FileOutputStream out=new FileOutputStream(imgFile);
			bitmap.compress(CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();
		}catch(Exception ex){
		}
		return fileName;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 /**
     * put string preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public void putString(String key, String value) {
    	sharePref.edit().putString(key, value).commit();
    }

    /**
     * get string preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
     *         name that is not a string
     * @see #getString(Context, String, String)
     */
    public String getString(String key) {
        return getString( key, "");
    }

    /**
     * get string preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a string
     */
    public String getString(String key, String defaultValue) {
        return sharePref.getString(key, defaultValue);
    }

	
	
	
	 /**
     * put int preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public void putInt( String key, int value) {
        sharePref.edit().putInt(key, value).commit();
    }

    /**
     * get int preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a int
     * @see #getInt(Context, String, int)
     */
    public int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * get int preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a int
     */
    public int getInt(String key, int defaultValue) {
    	
        return sharePref.getInt(key, defaultValue);
    }

    /**
     * put long preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public void putLong( String key, long value) {
        sharePref.edit().putLong(key, value).commit();
    }

    /**
     * get long preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a long
     * @see #getLong(Context, String, long)
     */
    public long getLong( String key) {
        return getLong(key, -1);
    }

    /**
     * get long preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a long
     */
    public long getLong( String key, long defaultValue) {
        return sharePref.getLong(key, defaultValue);
    }

    /**
     * put float preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public void putFloat( String key, float value) {
        sharePref.edit().putFloat(key, value).commit();
    }

    /**
     * get float preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a float
     * @see #getFloat(Context, String, float)
     */
    public float getFloat( String key) {
        return getFloat(key, -1);
    }

    /**
     * get float preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a float
     */
    public float getFloat( String key, float defaultValue) {
        return sharePref.getFloat(key, defaultValue);
    }

    /**
     * put boolean preferences
     * 
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public void putBoolean( String key, boolean value) {
        sharePref.edit().putBoolean(key, value).commit();
    }

    /**
     * get boolean preferences, default is false
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
     *         name that is not a boolean
     * @see #getBoolean(Context, String, boolean)
     */
    public boolean getBoolean( String key) {
        return getBoolean(key, false);
    }

    /**
     * get boolean preferences
     * 
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a boolean
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sharePref.getBoolean(key, defaultValue);
    }
	
}
